package com.compass.mobile.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.BaseGridView
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import com.compass.mobile.callback.OnItemClickListener
import com.compass.mobile.databinding.FragmentLectureColumnBinding
import com.compass.mobile.framework.base.BaseVBFragment
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.utils.FocusViewUtils
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.Title
import com.compass.mobile.presenters.LectureColumnPresenter
import com.compass.mobile.widget.MyBridgeAdapter

/**
 * Description： 讲座专栏
 * Date:2024/5/22 14:51
 */
class LectureColumnFragment : BaseVBFragment<FragmentLectureColumnBinding>() {
    private val COLUMNS = 2
    override fun initView(view: View, savedInstanceState: Bundle?) {
        mBinding?.caseItem?.post {
            // 使用获取到的宽度
            val width = mBinding?.caseItem?.width
            width?.let {
                specialColumn(it)
            }
        }
//        FocusViewUtils.scaleView(mBinding?.tvPrevious, 0)
//        FocusViewUtils.scaleView(mBinding?.tvNextPage, 0)

    }

    override fun intListener() {
        mBinding?.tvPrevious?.onClick {
            showToast("上一页")
        }

        mBinding?.tvNextPage?.onClick {
            showToast("下一页")
        }
    }


    private val onItemClickListener: OnItemClickListener = object : OnItemClickListener {
        override fun onItemClick(position: Int,view: View) {
//            val navController= Navigation.findNavController(view)
//            navController.navigate(R.id.action_homeCareFragment_to_caseDetailsFragment)


        }
    }

    @SuppressLint("RestrictedApi")
    fun specialColumn(width: Int) {
        mBinding?.caseItem?.setNumColumns(COLUMNS)
        mBinding?.caseItem?.horizontalSpacing = dpToPx(10)
        mBinding?.caseItem?.verticalSpacing = dpToPx(10)
        val finalWidth = width - dpToPx(80)
        val lectureColumnPresenter =
            LectureColumnPresenter()
        lectureColumnPresenter.setFinalWidth(finalWidth / 2)
        val arrayObjectAdapter = ArrayObjectAdapter(lectureColumnPresenter)
        val itemBridgeAdapter = MyBridgeAdapter(
            requireContext(),
            arrayObjectAdapter, onItemClickListener
        )
        mBinding?.caseItem?.setAdapter(itemBridgeAdapter)
        mBinding?.caseItem?.setFocusScrollStrategy(BaseGridView.FOCUS_SCROLL_ITEM)
        arrayObjectAdapter.addAll(0, specialColumns)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_SMALL, false
        )


    }


    companion object {


        val specialColumns: List<Title?>
            get() {
                val titleList: MutableList<Title?> = ArrayList()
                titleList.add(Title("胃痛的症状和治疗方法? 胃痛怎么回事?"))
                titleList.add(Title("胃痛的症状和治疗方法? 胃痛怎么回事?"))
                titleList.add(Title("胃痛的症状和治疗方法? 胃痛怎么回事?"))
                titleList.add(Title("胃痛的症状和治疗方法? 胃痛怎么回事?"))
                titleList.add(Title("胃痛的症状和治疗方法? 胃痛怎么回事?"))
                titleList.add(Title("胃痛的症状和治疗方法? 胃痛怎么回事?"))
                titleList.add(Title("胃痛的症状和治疗方法? 胃痛怎么回事?"))
                titleList.add(Title("胃痛的症状和治疗方法? 胃痛怎么回事?"))
                titleList.add(Title("胃痛的症状和治疗方法? 胃痛怎么回事?"))
                titleList.add(Title("胃痛的症状和治疗方法? 胃痛怎么回事?"))
                return titleList
            }
    }
}